//
//  BiDocuEngine.m
//  TrunkIndustryManageSystem
//
//  Created by WT_lyy on 15/5/6.
//  Copyright (c) 2015年 李璐西. All rights reserved.
//

#import "BiDocuEngine.h"


#import "BiNetEngine.h"
#import "ASIFormDataRequest.h"
#import "BiPurchaseParser.h"
#import "ASINetworkQueue.h"

#import "BiDocuParser.h"

@implementation BiDocuEngine
SINGLE_INSTANCE_IMPLEMENTION(BiDocuEngine)


/**
 *   //  列表， 下拉刷新请求更多，dict为传入参数字典
 */
- (void)requestDocmengList:(id)delegate paraDict:(NSDictionary *)dict{
    
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarLicenseCenter/getCommonList", SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_CAR_DOCU_LIST]];
    
    [self createPostRequest:url paramDic:dict tag:NET_GET_CAR_DOCU_LIST];
}

/**
 *   //  牌证开缴款单审核，dict为传入参数字典
 */
- (void)requestDocBillCheck:(id)delegate paraDict:(NSDictionary *)dict{
    
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarLicenseCenter/modify",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_CAR_DOC_CHECK]];
    
    [self createPutRequest:url paramDic:dict tag:NET_GET_CAR_DOC_CHECK];
}

/**
 *   //  牌证信息审核，dict为传入参数字典
 */
- (void)requestDocInfoManageCheck:(id)delegate paraDict:(NSDictionary *)dict{
    
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarLicenseCenter/modify",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_CAR_DOC_CHECK]];
    
    [self createPutRequest:url paramDic:dict tag:NET_GET_CAR_DOC_CHECK];
}

/**
 *   //  牌证信息上传，dict为传入参数字典
 */
- (void)requestDocInfoUploadCheck:(id)delegate paraDict:(NSDictionary *)dict{
    
    NSString *url = [NSString stringWithFormat:@"%@/mobile.php/CarLicenseCenter/modify",SERVICE_URL];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_CAR_DOC_CHECK]];
    
    [self createPutRequest:url paramDic:dict tag:NET_GET_CAR_DOC_CHECK];
}

- (void)requestDone:(ASIHTTPRequest *)request
{
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    int httpCode = [request responseStatusCode];
    
    if (200 > httpCode || 300 <= httpCode)
    {
        NSLog(@"http error --- tag = %d, http code = %d", tag, httpCode);
        assert(0);
        return;
    }
    
    switch (tag)
    {
        case NET_GET_CAR_DOCU_LIST:
        {
            [self onGetDocumentListData:request];
        }
            break;
            
        case NET_GET_CAR_DOC_CHECK:
        {
            [self onGetDocumentCommonkData:request];
        }
            break;
        default:
            break;
    }
    
}


- (void)onGetDocumentListData:(ASIHTTPRequest *)request
{
    int httpCode = [request responseStatusCode];
    
    if (200 != httpCode)
    {
        return;
    }
    
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    id delegate = [_dataDic objectForKey:[NSNumber numberWithInt:tag]];
    [_dataDic removeObjectForKey:[NSNumber numberWithInt:tag]];  // 请求完后清除掉delegate
    
    if (nil != delegate && [delegate respondsToSelector:@selector(onNetEngineResponse:entity:userInfo:)])
    {
        id  entity  = [BiDocuParser  parserDoceuListEntity:[request responseString]];
        [delegate onNetEngineResponse:tag entity:entity userInfo:nil];
    }
}



- (void)onGetDocumentCommonkData:(ASIHTTPRequest *)request
{
    int httpCode = [request responseStatusCode];
    
    if (200 != httpCode)
    {
        return;
    }
    
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    id delegate = [_dataDic objectForKey:[NSNumber numberWithInt:tag]];
    [_dataDic removeObjectForKey:[NSNumber numberWithInt:tag]];  // 请求完后清除掉delegate
    
    if (nil != delegate && [delegate respondsToSelector:@selector(onNetEngineResponse:entity:userInfo:)])
    {
        id  entity  = [BiDocuParser  parserDoceuCommonEntity:[request responseString]];
        [delegate onNetEngineResponse:tag entity:entity userInfo:nil];
    }
}


@end
